﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Toolkit.HasPower(Powers.SystemManager))
            {
                //FetchQuestionToVerify();
            }
            
            if (Toolkit.HasPower(Powers.Teacher))
            {
                FetchInvite();
                FetchQuestionToAnswer();
            }
            
            if (Toolkit.HasPower(Powers.TeacherVerifier))
            {
                FetchTeacherToVerify();
            }

            if (Toolkit.HasPower(Powers.ArticleVerifier))
            {
                //FetchArticleToVerify();
            }
        }
    }

    private void FetchArticleToVerify()
    {
        CmsDataContext db = new CmsDataContext();
        int count = (from aA in db.Article
                     where aA.Belong == Toolkit.CurrentUserBelong &&
                     aA.Verified == null
                     select aA).Count();

        ArticleToVerify.Text = string.Format("{0} 篇文章需要审核。", count);
        ArticleToVerify.Visible = count > 0;
    }

    private void FetchTeacherToVerify()
    {
        CmsDataContext db = new CmsDataContext();
        int count = (from aT in db.Teacher
                     where aT.Belong == Toolkit.CurrentUserBelong &&
                     aT.Verfied == null
                     select aT).Count();

        TeacherToVerify.Text = string.Format("{0} 位教师信息需要审核。", count);
        TeacherToVerify.Visible = count > 0;
    }

    private void FetchQuestionToAnswer()
    {
        CmsDataContext db = new CmsDataContext();
        int count = (from aQ in db.Question
                     where aQ.Verified != null &&
                     aQ.Teachers.Contains(Toolkit.CurrentTeacherID) &&
                     !aQ.Answer.Select(aA => aA.TeacherID).Contains(Toolkit.CurrentTeacherID)
                     select aQ).Count();

        QuestionToAnswer.Text = string.Format("{0} 个问题需要回答。", count);
        QuestionToAnswer.Visible = count > 0;
    }

    private void FetchInvite()
    {
        DateTime now = DateTime.Now;
        CmsDataContext db = new CmsDataContext();
        int count = (from aI in db.Invite
                     where aI.TeacherID == Toolkit.CurrentTeacherID &&
                     aI.Accepted == null &&
                     now.CompareTo(aI.Invited.AddDays(3)) >= 0
                     select aI).Count();

        InviteToConfirm.Text = string.Format("{0} 个授课邀请。", count);
        InviteToConfirm.Visible = count > 0;
    }

    private void FetchQuestionToVerify()
    {
        CmsDataContext db = new CmsDataContext();
        int count = (from aQ in db.Question
                 where aQ.Verified == null
                 select aQ).Count();

        QuestionToVerify.Text = string.Format("{0} 个在线问题需要审核。", count);
        QuestionToVerify.Visible = count > 0;
    }
    protected void Exit_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Response.Redirect("./Login.aspx");
    }
}